package ai;

import misc.Debug;
import ai.engine.AIP;
import ai.engine.AIPlayer;
import ai.engine.factories.TaskFactory;
import engine.GameEngine;
import engine.data.Map;
import engine.data.Player;
import engine.data.Unit;
import engine.data.UnitType;
import util.Iterator;
import util.List;

public class Util {

	// CONFIG
	public static boolean debug = true;

	public static boolean isOnSameTile(Localizable stuff1, Localizable stuff2) {
		return stuff1.getX() == stuff2.getX() && stuff1.getY() == stuff2.getY();
	}

	public static boolean isOnSameTile(short[] stuff1, Localizable stuff2) {
		return stuff1[0] == stuff2.getX() && stuff1[1] == stuff2.getY();
	}

	public static Localizable getBestBuildingSpot(Player player, Map map, byte cityObjective) {
		// TODO Faire l'algo qui trouve le meileur endroit ou construire
		//if (player.cities.size()==0){
		if (player.units.size() != 0)
			return new Node(((Unit) player.units.get(0)).x, ((Unit) player.units.get(0)).y);
		return new Node((short) 0, (short) 0);
	}

	public static byte getBestMouvementUnitType() {
		List possibleUnitTypes = GameEngine.getPossibleUnits();
		Iterator it = possibleUnitTypes.iterator();
		byte unitType;
		byte ret = UnitType.NONE;
		short bestMP = 0;
		while (it.hasNext()) {
			unitType = ((Byte) it.next()).byteValue();
			if (UnitType.getMP(unitType) > bestMP) {
				bestMP = UnitType.getMP(unitType);
				ret = unitType;
			}

		}
		return ret;
	}

	public static byte getBestAttackUnitType() {
		List possibleUnitTypes = GameEngine.getPossibleUnits();
		Iterator it = possibleUnitTypes.iterator();
		byte unitType;
		byte ret = UnitType.NONE;
		byte bestAP = 0;
		while (it.hasNext()) {
			unitType = ((Byte) it.next()).byteValue();
			if (UnitType.getAP(unitType) > bestAP) {
				bestAP = UnitType.getAP(unitType);
				ret = unitType;
			}

		}
		return ret;
	}

	public static byte getBestDefenseUnitType() {
		List possibleUnitTypes = GameEngine.getPossibleUnits();
		Iterator it = possibleUnitTypes.iterator();
		byte unitType;
		byte ret = UnitType.NONE;
		byte bestDP = 0;
		while (it.hasNext()) {
			unitType = ((Byte) it.next()).byteValue();
			if (UnitType.getDP(unitType) > bestDP) {
				bestDP = UnitType.getDP(unitType);
				ret = unitType;
			}

		}
		return ret;
	}

	public static AIP getCurrentAIP() {
		return getCurrentAIPlayer().getAIP();
	}

	public static AIPlayer getCurrentAIPlayer() {
		return ((AIPlayer) GameEngine.currPlayer);
	}

	public static void AIDebug(String message) {
		if (debug)
			Debug.logConsole("AI - " + message);
	}
}
